package com.visizen.wisdomclass.server.action.User;

import com.alibaba.fastjson.JSONObject;
import com.visizen.wisdomclass.server.action.response.Result;
import com.visizen.wisdomclass.server.annotation.ValidateToken;
import com.visizen.wisdomclass.server.model.WcUser;
import com.visizen.wisdomclass.server.service.UserService;
import com.visizen.wisdomclass.server.token.WCToken;
import com.visizen.wisdomclass.server.token.WCTokenManager;
import com.visizen.wisdomclass.server.util.WCDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户 获取用户个人信息
 *
 * @author rankun203@gmail.com
 */
@Controller
@RequestMapping("/User")
public class UserGetMyInfoAction {
    /**
     * 获取用户个人信息, 获取用户自己的个人信息，只能获取自己的信息<br/>
     */
    @ValidateToken
    @RequestMapping(value = "/getMyInfo", method = RequestMethod.GET)
    @ResponseBody
    public Result getMyInfo(@RequestParam String token, HttpServletRequest request) {
        //开始构造返回对象
        Result result = new Result();
        //结果对象里面的payLoad或是debug对象
        JSONObject jo = new JSONObject();

        WCToken tokenObj = tokenManager.getToken(token);
        WcUser user = null;
        Object obj = tokenObj.getSource();
        if (obj instanceof WcUser) {
            user = (WcUser) obj;
        }
        jo.put("userId", user.getUser_id());
        jo.put("fullname", user.getFullname());
        jo.put("attachId", user.getAttach_id());
        jo.put("sex", user.getSex());
        jo.put("birthday", WCDate.toDateString(user.getBirthday()));
        jo.put("mobilephone", user.getMobilephone());
        jo.put("email", user.getEmail());
        result.setSuccessResult(request, "", jo);
        return result;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    @Autowired
    public void setTokenManager(WCTokenManager tokenManager) {
        this.tokenManager = tokenManager;
    }

    private UserService userService;
    private WCTokenManager tokenManager;

}
